﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Drawing.Drawing2D;

namespace UMLDesigner
{
    partial class MainWindow : Form
    {
        private Pen dashed = new Pen(Pens.Silver.Brush);

        public MainWindow()
        {
            InitializeComponent();

            dashed.DashStyle = DashStyle.Dash;
        }

        private void OnKeyPress(object sender, KeyPressEventArgs e)
        {

        }

        public void OnMouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            Control c = (Control)sender;
            Console.WriteLine(c);
        }

        public void OnMouseDownEventHandler(object sender, MouseEventArgs e)
        {
            Control c = (Control)sender;
            Console.WriteLine(c);
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            mainPanel.Panel1.MouseMove += new MouseEventHandler(OnMouseMoveHandler);
            mainPanel.Panel1.MouseDown += new MouseEventHandler(OnClickEventHandler);
        }

        private void OnMouseMoveHandler(object sender, MouseEventArgs e)
        {
            Graphics g = mainPanel.Panel1.CreateGraphics();

            Pen pen = new Pen(Pens.Red.Brush);
            pen.DashStyle = DashStyle.Dash;

            PaintRelations();
            g.DrawLine(dashed, 0, e.Y, mainPanel.Panel1.Width, e.Y);
            g.DrawLine(dashed, e.X, 0, e.X, mainPanel.Panel1.Height);
        }

        private void OnClickEventHandler(object sender, MouseEventArgs e)
        {
            mainPanel.Panel1.MouseMove -= new MouseEventHandler(OnMouseMoveHandler);
            mainPanel.Panel1.MouseDown -= new MouseEventHandler(OnClickEventHandler);

            ClassView clase = new ClassView(e.X, e.Y);
            WindowsManager.AddClass(clase);
            mainPanel.Panel1.Controls.Add(clase.GetControl());

            //Repaint the relations
            PaintRelations();
        }

        public void DeleteClass(ClassView cv)
        {
            mainPanel.Panel1.Controls.Remove(cv.GetControl());
        }

        public FormControl GetControlFormulario()
        {
            return formControl;
        }

        public void PaintRelations()
        {
            Graphics g = mainPanel.Panel1.CreateGraphics();
            ConfigureGraphics(g);

            foreach (RelationView rel in WindowsManager.GetRelations())
            {
                rel.Paint(g);
            }

            FreeGraphics(g);
        }

        private void mainPanel_Panel1_Paint(object sender, PaintEventArgs e)
        {
            PaintRelations();
        }

        private void ConfigureGraphics(Graphics g)
        {
            g.Clear(Color.White);
            g.SmoothingMode = SmoothingMode.AntiAlias;
        }

        private void FreeGraphics(Graphics g)
        {
            g.Dispose();
        }

        private void inheritanceMenuOption_Click(object sender, EventArgs e)
        {
            WindowsManager.SetRelationToCreate(new InheritanceView());
        }

        private void opcionMenuAgregacion_Click(object sender, EventArgs e)
        {
            WindowsManager.SetRelationToCreate(new AggregationView());
        }

        private void opcionMenuComposicion_Click(object sender, EventArgs e)
        {
            WindowsManager.SetRelationToCreate(new CompositionView());
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                WindowsManager.DeselectClass();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnExportToImage_Click(object sender, EventArgs e)
        {
            //Ocultamos la barra de menú para que no aparezca en la imagen
            toolStrip1.Hide();

            //Deseleccionamos la clase seleccionada si la hubiera para que no aparezca resaltada
            WindowsManager.DeselectClass();
            
            Panel panel = mainPanel.Panel1;

            panel.Refresh();

            //TODO Se puede mejorar la exportación buscando la menor (x,y) que tenga una clase
            //y la mayor (x,y) y formar un rectangulo con esas coordenadas. De esta forma eliminamos
            //el espacio en blanco sobrante, exportando el área minima para mostrar todas las clases
            //Dejar margenes si se considera oportuno...

            Rectangle bounds = panel.Bounds;
            Point pt = panel.PointToScreen(bounds.Location);
            Bitmap bmp = new Bitmap(bounds.Width, bounds.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(new Point(pt.X - panel.Location.X, pt.Y - panel.Location.Y), Point.Empty, bounds.Size);

            //Mostramos la barra de menú
            toolStrip1.Show();

            SaveFileDialog svDailog = new SaveFileDialog();
            svDailog.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)*.gif|Bitmaps (*.bmp)*.bmp";
            svDailog.FilterIndex = 1;
            DialogResult dr = svDailog.ShowDialog();
            if (dr.Equals(DialogResult.OK))
            {
                string _filename = svDailog.FileName;
                bmp.Save(_filename);
            }

            bmp.Dispose();
            g.Dispose();
        }

    }
}
